/**
 * 渠道管理相关接口
 * @author 张效玮2 yythis@163.com
 * @date 2025-01-15
 */

import type * as ChannelManagement from "./type"
import { request } from "@/http/axios"

/** 创建渠道服务商 */
export function createChannelListApi(data: ChannelManagement.CreateChannelRequestData) {
  return request<ChannelManagement.ChannelDetailResponseData>({
    url: "/account/channel/service-provider",
    method: "post",
    data
  })
}

/** 获取渠道服务商详情 */
export function getChannelDetailApi(id: string) {
  return request<ChannelManagement.ChannelDetailResponseData>({
    url: `/channel-management/channels/${id}`,
    method: "get"
  })
}

/** 获取渠道服务商列表 */
export function getChannelListApi(data: ChannelManagement.CreateChannelRequestData) {
  return request<ChannelManagement.ChannelDetailResponseData>({
    url: "/account/channel/service-provider/page",
    method: "post",
    data
  })
}

/** 更新渠道服务商 */
export function updateChannelApi(data: ChannelManagement.UpdateChannelRequestData) {
  return request({
    url: `/channel-management/channels/${data.id}`,
    method: "put",
    data
  })
}

/** 删除渠道服务商 */
export function deleteChannelApi(id: string) {
  return request({
    url: `/channel-management/channels/${id}`,
    method: "delete"
  })
}

/** 获取代理商列表 */
export function getAgentListApi(params: {
  /** 渠道ID */
  channelId: string
  /** 代理商级别 */
  level?: "second" | "third"
  /** 父级代理商ID（查询三级代理时） */
  parentId?: string
}) {
  return request<ChannelManagement.AgentListResponseData>({
    url: "/channel-management/agents",
    method: "get",
    params
  })
}

/** 创建二级代理商 */
export function createSecondAgentApi(data: ChannelManagement.CreateAgentRequestData) {
  return request({
    url: "/channel-management/agents/second",
    method: "post",
    data
  })
}

/** 创建三级代理商 */
export function createThirdAgentApi(data: ChannelManagement.CreateAgentRequestData) {
  return request({
    url: "/channel-management/agents/third",
    method: "post",
    data
  })
}

/** 更新代理商信息 */
export function updateAgentApi(data: ChannelManagement.UpdateAgentRequestData) {
  return request({
    url: `/channel-management/agents/${data.id}`,
    method: "put",
    data
  })
}

/** 删除代理商 */
export function deleteAgentApi(id: string) {
  return request({
    url: `/channel-management/agents/${id}`,
    method: "delete"
  })
}

/** 切换代理商状态 */
export function toggleAgentStatusApi(id: string, status: "active" | "inactive") {
  return request({
    url: `/channel-management/agents/${id}/status`,
    method: "patch",
    data: { status }
  })
}
